<template>
  <BreadCrumbVue />
  <section class="search-bar">
    <el-row justify="space-between">
      <el-col :span="2">
        <el-icon>
          <search />
        </el-icon>
        <span>筛选搜索</span>
      </el-col>
      <el-col :span="3">
        <el-button>重置</el-button>
        <el-button type="primary" @click="queryAdminList">查询搜索</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="5" class="flex">
        <span>账号/姓名:</span>
        <el-input
          placeholder="请输入账号/姓名"
          style="width:200px;"
          v-model="searchParams.keyword"
          @keydown.enter="queryAdminList"
        ></el-input>
      </el-col>
    </el-row>
  </section>
  <section class="add-bar">
    <el-row justify="space-between">
      <el-col :span="2">
        <el-icon>
          <document />
        </el-icon>
        <span>数据列表</span>
      </el-col>
      <el-col :span="1">
        <el-button type="primary" @click="addUserFn">添加</el-button>
      </el-col>
    </el-row>
  </section>
  <!-- 下面是用户展示数据的表格 -->
  <el-table :data="tableData" border style="width: 100%">
    <el-table-column prop="id" label="编号" width="120" />
    <el-table-column prop="username" label="账号" />
    <el-table-column prop="nickName" label="姓名" />
    <el-table-column prop="email" label="邮箱" />
    <el-table-column prop="createTime" label="添加时间">
      <template #default="scope">{{ formatDate(scope.row.createTime) }}</template>
    </el-table-column>
    <el-table-column prop="loginTime" label="最后登录">
      <template #default="scope">{{ formatDate(scope.row.loginTime) }}</template>
    </el-table-column>
    <el-table-column prop="status" label="是否启用">
      <template #default="scope">
        <el-switch
          v-model="scope.row.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
          @change="statusChange(scope.row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button
          type="text"
          size="small"
          @click="allocRoleFn(scope.row)"
          v-permisson="'allocRole'"
        >分配角色</el-button>
        <el-button type="text" size="small" @click="editUserFn(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <section class="pager">
    <el-pagination
      v-model:currentPage="searchParams.pageNum"
      v-model:page-size="searchParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="state.total"
      prev-text="上一页"
      next-text="下一页"
      @current-change="pageChange"
    ></el-pagination>
  </section>
  <editUserVue :title="optTitle" :is-visable="isDialogShow" :edit-id="editId" @myclose="closeFn" />
  <allocRoleVue :is-visable="isAllocRoleShow" :user-id="state.userId" @myclose="allocRoleClose" />
</template>
<script setup lang='ts'>
import BreadCrumbVue from '@/components/BreadCrumb.vue';
import { QueryAdminListApi, UpdateUserStatusApi } from '@/request/api';
import { Search, Document } from '@element-plus/icons-vue'
import { reactive, ref, toRefs } from 'vue';
import { formatDate } from '@/util/utils'
import { ElMessage } from 'element-plus';
import editUserVue from './components/editUser.vue';
import allocRoleVue from './components/allocRole.vue';

const searchParams = reactive<IQueryAdminListParams>({
  // 搜索框的内容
  keyword: '',
  // 当前是第几页数据
  pageNum: 1,
  // 一次请求多少条数据回来
  pageSize: 10
})

interface AdminState {
  tableData: IUserModel[];
  total: number;
  editId: number;
  userId: number;
  optTitle: string;
}

const state = reactive<AdminState>({
  tableData: [],
  total: 0,
  editId: 0,
  userId: 0,
  optTitle: '添加用户'
})

const queryAdminList = () => {
  QueryAdminListApi(searchParams).then(res => {
    if (res.code === 200) {
      state.tableData = res.data.list
      state.total = res.data.total
    }
  })
}
queryAdminList()

const pageChange = () => {
  // 请求新数据，展示在页面中
  queryAdminList();
}

const statusChange = (row: IUserModel) => {
  // 请求更改用户的状态
  UpdateUserStatusApi(row.id, row.status).then(res => {
    if (res.code === 200) {
      ElMessage.success(res.message)
    } else {
      ElMessage.error(res.message)
    }
  })
}

const isDialogShow = ref(false);
const isAllocRoleShow = ref(false);

const addUserFn = () => {
  isDialogShow.value = true
  state.optTitle = '添加用户'
}

const editUserFn = (row: IUserModel) => {
  // 显示dialog
  isDialogShow.value = true
  // 把id传递给 diaolog，让dialog在里面请求最新的数据
  state.editId = row.id
  state.optTitle = '编辑用户'
}

const closeFn = (isReload: boolean) => {
  isDialogShow.value = false;
  // 重置editId
  state.editId = 0
  // 判断，是否需要刷新
  isReload && queryAdminList()
}

const allocRoleFn = (row: IUserModel) => {
  state.userId = row.id;
  isAllocRoleShow.value = true;
}

const allocRoleClose = () => {
  // 把用户id重置
  state.userId = 0;
  // 显示dialog的变量重置
  isAllocRoleShow.value = false
}


// 把权限控制到按钮级别
// 其实第一次请求回来的权限数据，不仅仅包含 左侧的 菜单数据，还包含页面里面的按钮级的权限数据
// 如果 hidden 是 0，就是菜单权限，hidden不是0，就是 按钮级权限
// 如何控制 按钮级权限
// 判断，登录后返回的数据里面，是否有对应 按钮级权限，如果有就保留按钮，如果没有，就删除对应的按钮
// 这里使用  自定义指令 的方式解决


const { tableData, editId, optTitle } = toRefs(state)
</script>
<style lang="less" scoped>
.search-bar,
.add-bar {
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 0 4px 2px #ccc;
  font-size: 12px;
  margin: 20px 0;
}
.pager {
  display: flex;
  justify-content: flex-end;
  padding-top: 20px;
}
</style>